<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回调</title>

    <!--引入Vue + axios-->
    <script src="js/plugins/vue/dist/vue.js"></script>
    <script src="js/plugins/axios/dist/axios.js"></script>
    <!--全局使用-->
    <script src="js/common.js"></script>
</head>
<body>
<div id="myDiv">

</div>
<script type="text/javascript">
    /*
        回调页面一共要做3件事:
        1.拿到回调页面的地址,进行解析,获取微信返回的code码
        2.拿到解析后的code码向后端微信登录的接口传递回去
        3.后端通过这个code取得微信的资源库的临时访问凭证token与openId
     */
    new Vue({
        el:"#myDiv",
        mounted(){
            //解析参数对象
            let url = location.href;//http://bugtracker.itsource.cn/callback.html?code=001vh70w3G9gIY22d40w3ML6Gi1vh703&state=1
            let paramObj = parseUrlParams2Obj(url);//code = 001vh70w3G9gIY22d40w3ML6Gi1vh703
            let params = {"code":paramObj.code};//params = 001vh70w3G9gIY22d40w3ML6Gi1vh703
            //发起微信登录请求
            this.$http.post("/login/wechat",params)
                .then(result=>{
                    result = result.data;
                    if(result.success){
                        //做登录
                        //提示
                        let {token,logininfo} = result.resultObj;
                        //1.token + logininfo 存储到localStorage
                        localStorage.setItem("token",token);
                        localStorage.setItem("logininfo",JSON.stringify(logininfo));
                        //2.跳转到主页 ，还要在主页显示当前登录人
                        //登录成功，跳到首页
                        location.href="index.html";

                    }else{ //没有关联跳转关联页面
                        //获取发送请求参数
                        // let binderUrl = "http://bugtracker.itsource.cn/binder.html"+result.resultObj; 密码绑定且无后端校验
                        let binderUrl = "http://bugtracker.itsource.cn/binder2emai.html"+result.resultObj; //手机验证码绑定,不用校验
                        location.href=binderUrl;
                    }
                })
                .catch(result=>{
                    alert("系统错误");
                    console.log(result);
                })
        }
    });
</script>
</body>
</html>